using System;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XNA_2DFramwork;
using FarseerGames.FarseerPhysics.Dynamics;


namespace ArtificialIntelligence.Racing
{
    public class Racer:ComputerAi
    {
        float speed = 10;
        float RacerLenght;
        SpriteFont Font;
        Vector2 pos = new Vector2(10000, 0);
        Texture2D VisualTexture2;

        public Texture2D CollectTexture;
        public Geom CollectGeom;
        public Body CollectBody;
        public Vector2 CollectOrigin;
        
        //----------------
        public Texture2D DetectTexture;
        public Geom DetectGeom;
        public Body DetectBody;
        public Vector2 DetectOrigin;

        public Racer(Game game, Vector2 position, string filename, string GameOverName, PhysicsSimulator PhysicsSimulator)
            : base(game)
        {
            Type = ObjectType.Racer;
            Texture = game.Content.Load<Texture2D>(filename);
            Origin = new Vector2(Texture.Width/2f, Texture.Height/2f);
            Width = Texture.Width;
            Height = Texture.Height;
            Position = position;
            Body = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, Width, Height, 1);
            Body.Position = position;
            Geom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSimulator, Body, Width, Height);
            Font = game.Content.Load<SpriteFont>("SpriteFont1");
            this.VisualTexture2 = game.Content.Load<Texture2D>(GameOverName);
            Body.IgnoreGravity = true;
            Geom.CollisionResponseEnabled = false;
            Geom.FrictionCoefficient = 1;
            SpriteBatch = new SpriteBatch(Game.GraphicsDevice);
            //-----------------------------
            CollectTexture = DemoBaseXNA.DrawingSystem.DrawingHelper.CreateRectangleTexture(Game.GraphicsDevice, 1000, 250, Color.TransparentBlack);
            CollectOrigin = new Vector2(600, 125);
            CollectBody = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, 1000, 250, 1);
            CollectBody.Position = new Vector2(600, (int)Position.Y);
            CollectGeom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSimulator, CollectBody, 1000, 250);
            CollectBody.IgnoreGravity = true;
            CollectGeom.CollisionResponseEnabled = false;
            //----------------------------
            DetectTexture = DemoBaseXNA.DrawingSystem.DrawingHelper.CreateRectangleTexture(Game.GraphicsDevice, Width, Height+100, Color.TransparentWhite);
            DetectOrigin = new Vector2(Width / 2f, (Height + 100)/2f);
            DetectBody = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, Width, Height + 100, 1);
            DetectBody.Position = Position;
            DetectGeom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSimulator, DetectBody, Width, Height + 100);
            DetectBody.IgnoreGravity = true;
            DetectGeom.CollisionResponseEnabled = false;
        }

        public override void Draw()
        {
            // SpriteBatch.Draw(Texture, Body.Position, r, Color.White, Body.Rotation, Origin,1f,
            //    SpriteEffects.None,0f);
            this.SpriteBatch.Begin();
            this.SpriteBatch.Draw(Texture, Body.Position,null, Color.Yellow,Body.Rotation,
                Origin,1f,SpriteEffects.None,0f);
           
            this.SpriteBatch.Draw(CollectTexture, CollectBody.Position, null, Color.Yellow, CollectBody.Rotation,
                CollectOrigin, 1f, SpriteEffects.None, 0f);

            this.SpriteBatch.Draw(DetectTexture, DetectBody.Position, null, Color.Yellow, DetectBody.Rotation,
                DetectOrigin, 1f, SpriteEffects.None, 0f);
            
            this.SpriteBatch.Draw(VisualTexture2, pos, Color.White);
            
            this.SpriteBatch.DrawString(Font, "Racer:" + Position.Y, new Vector2(10, 160), Color.Red);
            this.SpriteBatch.End();
        }
        public override void Update()
        {
            if (RacerLenght < City.RacerLenght)
            {
                this.Rectangle_AI = new Rectangle((int)this.Position.X, (int)this.Position.Y + 50, this.Texture.Width, this.Texture.Height + 100);
                this.CollectionRectangle = new Rectangle(250, (int)Position.Y, 1000, 250);
                Collect();
                if (speed < 10)
                    speed += 0.1f;

                this.Position.Y -= speed - RacePlayer.playerS;
               //--------------
                Body.Position = Position;
                DetectBody.Position = Position;
                CollectBody.Position = new Vector2(600, Position.Y);
               //-----------------------
                RacerLenght += speed;
            }
            else
            {
                pos.X = 0;
                pos.Y = 0;
            }
             if (this.pos.X > 750)
                 this.Move(-50, 0);
             if (this.pos.X > 350)
                 this.Move(50, 0);
           

        }
        public void Move(float x, float y)
        {
            if (RacerLenght < City.RacerLenght)
            {
                if (x < 0)
                {
                    if (this.Position.X <= 250)
                        speed = 0;
                    else
                        this.Position.X += x;
                  
                }
                if (x > 0)
                {
                    if (this.Position.X >= 850)
                        speed = 0;
                    else
                        this.Position.X += x;
                  
                }

                this.Position.Y += y;
            }
        }
        protected void Collect()
        {
            float MoveX ;
            foreach (VisualObject obj in VisualObject._lVisualObject)
            {
                switch (obj.Type) 
                {
                    case ObjectType.Money:
                        {
                            if (VisualObject.IsCollide(ref this.CollectGeom, ref obj.Geom))//.Collision(this, this.CollectionRectangle, obj, obj.RECTANGLE)))
                            {
                                //if (this.Position.X > obj.Position.X)
                                //{
                                //    if (GO(this, obj)&&!InBetween(this,obj))
                                //    {
                                //        MoveX = this.Position.X - obj.Position.X;
                                //        this.Move(-MoveX, 0);
                                //        obj.Move(10000, 0);
                                //    }
                                //}
                                //else
                                //{
                                    if (GO(this, obj) && !InBetween(this, obj))
                                    {
                                        MoveX = obj.Position.X - this.Position.X;
                                        this.Move(MoveX, 0);
                                        obj.Move(10000, 0);
                                    }
                                //}
                            }
                            break;
                        }

                    case ObjectType.Nitrous:
                        {
                            if (VisualObject.IsCollide(ref this.CollectGeom, ref  obj.Geom))
                            {
                                //if (this.Position.X > obj.Position.X)
                                //{
                                //    if (GO(this, obj)&&!InBetween(this,obj))
                                //    {
                                //        MoveX = this.Position.X - obj.Position.X;
                                //        this.Move(-MoveX, 0);
                                //        obj.Move(10000, 0);
                                //    }
                                //}
                                //else 
                                //{
                                    if (GO(this, obj)&&!InBetween(this,obj))
                                    {
                                        MoveX = obj.Position.X - this.Position.X;
                                        this.Move(MoveX, 0);
                                        obj.Move(10000, 0);
                                    }
                                //}
                            }
                            break;
                        }
                    case ObjectType.Obstacles:
                        {
                            if (VisualObject.IsCollide(ref this.DetectGeom, ref obj.Geom))
                            {
                                if (this.pos.X > obj.Position.X)
                                {
                                    if (this.pos.X< 750)
                                        this.Move(50, 0);
                                    else
                                        this.Move(-100, 0);
                                }
                                else
                                {
                                    if (this.pos.X> 300)
                                        this.Move(-50, 0);
                                    else
                                        this.Move(100, 0);
                                }
                            }
                            break;
                        }
                    case ObjectType.Player:
                        {
                            if (VisualObject.IsCollide(ref this.DetectGeom, ref  obj.Geom))
                            {
                                if (this.pos.X > obj.Position.X)
                                {
                                    if (this.pos.X < 750)
                                        this.Move(50, 0);
                                    else
                                        this.Move(-100, 0);
                                }
                                else
                                {
                                    if (this.pos.X > 300)
                                        this.Move(-50, 0);
                                    else
                                        this.Move(100, 0);
                                }
                            }
                            break;
                        }
                }
            }
        }
        private bool GO(VisualObject obj1 , VisualObject obj2 )
        {
            foreach (VisualObject obj in VisualObject._lVisualObject)
            {
                switch (obj.Type)
                {
                    case ObjectType.Obstacles:
                        {
                            if (IsCollide(ref obj.Geom, ref  obj2.Geom))
                                return false;
                            break;
                        }
                }
            }
            return true;
        }
        private bool InBetween(VisualObject obj1, VisualObject obj2)
        {
            foreach (VisualObject obj in _lVisualObject)
            {
                if (obj.Type == ObjectType.Obstacles)
                {
                    if (obj1.Position.Y > obj.Position.Y)
                        if (obj2.Position.Y < obj.Position.Y)
                            return true;
                    
                }
            }
            return false;
        }
    }
}